
-- local json = require "json"

if Assets == nil then
    Assets = {}
end
table.insert(Assets,     Asset("ANIM", "anim/npc_card_base.zip") )
table.insert(Assets,     Asset("ANIM", "anim/npc_cards.zip") )

-- local function load_card_name(name)
--     -- table.insert(Assets,      Asset("ATLAS", "images/cards/"..name..".xml")                         )
--     -- table.insert(Assets,      Asset("IMAGE", "images/cards/"..name..".tex")                         )
--     table.insert(Assets,      Asset("ATLAS", "images/inventoryimages/"..name..".xml")                         )
--     table.insert(Assets,      Asset("IMAGE", "images/inventoryimages/"..name..".tex")                         )
-- end

-- local cards_name = {
--     "npc_card_test"
-- }
-- for i, v in ipairs(cards_name) do
--     load_card_name(v)
-- end



local npc_cards_widget = require("widgets/npc_cards_widget")
--------------------------------
    -- AddClassPostConstruct("widgets/controls", function(self, owner)
    --     if owner == nil then
    --         return
    --     end

    --     local function ScaleTheWidget(tarWidget)
    --         if tarWidget == nil then
    --             return
    --         end
    --         local scrnw, scrnh = TheSim:GetScreenSize()
    --         local scaleNum = scrnh/1080
    --         tarWidget:SetScale(scaleNum)
    --     end
        
    --     -- owner:DoTaskInTime(0,function()

    --     --     -- self.The_Fate_widget = self:AddChild(The_Fate_widget(self.owner))
    --     --     -- self.The_Fate_widget:SetHAnchor(0)
    --     --     -- self.The_Fate_widget:SetVAnchor(0)
    --     --     -- self.The_Fate_widget:SetPosition(0,0)
    --     --     -- self.The_Fate_widget:Hide()
    --     --     self.show_flg = false


    --     owner:DoTaskInTime(0,function()
    --         local function Display_Cards(card_name)
    --             -- print("Display_Cards")
    --             if self.npc_cards_widget ~= nil then
    --                 self.npc_cards_widget:Kill()
    --             end

    --             self.npc_cards_widget = self:AddChild(npc_cards_widget(self.owner))
    --             self.npc_cards_widget:SetHAnchor(0)
    --             self.npc_cards_widget:SetVAnchor(0)
    --             self.npc_cards_widget:SetPosition(0,0)
    --             ScaleTheWidget(self.npc_cards_widget)
    --             self.npc_cards_widget:Create_Cards(card_name)
                

    --             if owner.__npc_cards_widget_scale_task then
    --                 owner.__npc_cards_widget_scale_task:Cancel()
    --             end
    --             owner.__npc_cards_widget_scale_task = owner:DoPeriodicTask(0.5,function()
    --                 ScaleTheWidget(self.npc_cards_widget)                
    --             end)

    --         end
            
    --         owner:ListenForEvent("npc_cards_sys.display",function(_,card_name)
    --             Display_Cards(card_name)
    --         end)
    --         owner:ListenForEvent("npc_cards_sys.kill",function()
    --             -- print("Remove Task")
    --             if self.npc_cards_widget and self.npc_cards_widget.Kill then
    --                 -- print("Remove Task 2")
    --                 if owner.__npc_cards_widget_scale_task then
    --                     owner.__npc_cards_widget_scale_task:Cancel()
    --                     owner.__npc_cards_widget_scale_task = nil
    --                 end
    --                 self.npc_cards_widget:Kill()
    --                 self.npc_cards_widget = nil
    --             end
    --         end)

    --     end)
    -- end)


    -- AddClientModRPCHandler("NPC_RPC_SYSTEM","npc_cards_sys.start",function(inst,data)
    --     inst:PushEvent("npc_cards_sys.display",data)
    -- end)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------- 玩家HUD,使用这个 方便手柄控制。界面显示的时候，角色不可走动。
AddClassPostConstruct("screens/playerhud",function(self, anim, owner)		
    ------ self  is  ThePlayer.HUD	
    ----------------------------------------------------------------------------------------------------------------
    self.Display_NPC_Cards = function(self,attach_player,card_name)
        if card_name == nil then
            return
            -- card_name = "npc_card_bottle_with_coin"
        end

        if self.temp_npc_cards_screen ~= nil then   ---- 万一流程BUG，先关掉现有 界面
            self:Close_NPC_Cards()
        end


        self.temp_npc_cards_screen = npc_cards_widget(self.owner,attach_player)
        self:OpenScreenUnderPause(self.temp_npc_cards_screen)    ----- 游戏交互控制权移交给界面。解除移交用 TheFrontEnd:PopScreen(self.temp_npc_cards_screen)

        self.temp_npc_cards_screen:SetHAnchor(0)
        self.temp_npc_cards_screen:SetVAnchor(0)
        self.temp_npc_cards_screen:SetPosition(0,0)
        self.temp_npc_cards_screen:Create_Cards(card_name)
        self.temp_npc_cards_screen:Scale_Task_Start()

    end

    self.Close_NPC_Cards = function(_)
        if self.temp_npc_cards_screen then
            self.temp_npc_cards_screen:Close()
            self.temp_npc_cards_screen = nil
        end
    end
    ----------------------------------------------------------------------------------------------------------------
end)


AddClientModRPCHandler("NPC_RPC_SYSTEM","npc_cards_sys.start",function(inst,data)
    inst:PushEvent("npc_cards_sys.display",data)
end)

AddPlayerPostInit(function(inst)
    inst:DoTaskInTime(0,function()
        if inst.HUD then
            inst:ListenForEvent("npc_cards_sys.display",function(_,card_name)
                inst.HUD:Display_NPC_Cards(inst,card_name)
            end)

            inst:ListenForEvent("npc_cards_sys.kill",function()
                inst.HUD:Close_NPC_Cards()
            end)
        end
    end)
end)